package demo.using;

/**
 * @author : GuoFei
 * @date : 2018/11/2 18:40
 */
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;

public class KeyEventDemo extends JFrame{
    private KeyboardPanel keyboardPanel = new KeyboardPanel();




    public static void getScreenImg(){
        try {
            Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
            BufferedImage screenshot = (new Robot()).createScreenCapture(new Rectangle(0,
                    0,(int)dimension.getWidth(),(int)dimension.getHeight()));
            File file = new File("C:\\Users\\mayn\\Desktop\\新建位图图像 (3).bmp");
            ImageIO.write(screenshot, "jpg", file);
        } catch (HeadlessException e) {
            e.printStackTrace();
        } catch (AWTException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private void getScreenImgs( HttpServletResponse response) throws Exception {
        Thread.sleep(3000);
        response.setContentType("image/jpg");
        ServletOutputStream sos = response.getOutputStream();
        // 禁止页面缓存
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        // 创建内存图象并获得其图形上下文
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        BufferedImage screenshot = (new Robot())
                .createScreenCapture(new Rectangle(0, 0, (int) dimension
                        .getWidth(), (int) dimension.getHeight()));
        // 将图像输出到客户端
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ImageIO.write(screenshot, "jpg", bos);
        byte[] buf = bos.toByteArray();
        response.setContentLength(buf.length);
        sos.write(buf);
        bos.close();
        sos.close();
    }



//    public KeyEventDemo(){
//        add( keyboardPanel);
//
//        keyboardPanel.setFocusable(true);
//
//    }



//    public static void main(String[] args){
//        KeyEventDemo frame = new KeyEventDemo();
//        frame.setTitle( "KeyEventDemo");
//        frame.setSize(300,300);
//        frame.setLocationRelativeTo( null);
//        frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
//        frame.setVisible( true);
//
//    }

    static class KeyboardPanel extends JPanel{
        private int x = 100;
        private int y = 100;
        private char KeyChar = 'A' ;

        public KeyboardPanel(){
            addKeyListener( new KeyAdapter(){
                public void keyPressed(KeyEvent e){
                    switch(e.getKeyCode()){
                        case KeyEvent.VK_DOWN: y += 10;break;
                        case KeyEvent.VK_UP: y -= 10; break;
                        case KeyEvent.VK_LEFT: x -= 10; break;
                        case KeyEvent.VK_RIGHT: x += 10; break;
                        default: KeyChar = e.getKeyChar();
                    }

                    repaint();
                }
            });
        }

        protected void paintComponent(Graphics g){
            super.paintComponent(g);

            g.setFont( new Font("TimesRoman" ,Font.PLAIN,24));
            g.drawString(String. valueOf(KeyChar), x, y);
        }
    }
}
